package com.jn.mvvm.viewmodel;

import androidx.lifecycle.LiveData;
import androidx.lifecycle.MutableLiveData;
import androidx.lifecycle.ViewModel;

import com.jn.mvvm.entity.Book;

import java.util.ArrayList;
import java.util.List;

public class BookViewModel2 extends ViewModel {
    private List<Book> mBookList = new ArrayList<>();
    private MutableLiveData<List<Book>> mBookListData = new MutableLiveData<>();

    public LiveData<List<Book>> getLiveData() {
        return mBookListData;
    }

    public List<Book> getBookList() {
        return mBookListData.getValue();
    }

    public void add(Book book) {
        Book newBook = new Book();
        newBook.setName(book.getName());
        newBook.setAuthor(book.getAuthor());
        mBookList.add(newBook);
        mBookListData.postValue(mBookList);
    }

    public int getCount() {
        List<Book> list = mBookListData.getValue();
        if (list == null) {
            return 0;
        }
        return list.size();
    }

    @Override
    protected void onCleared() {
        super.onCleared();
        //此处做资源回收等工作
    }
}
